package servlet;

import javabean.User;

import javax.servlet.annotation.WebServlet;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

@WebServlet("/loginServlet")
public class LoginServlet extends javax.servlet.http.HttpServlet {
    protected void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {
        request.setCharacterEncoding("UTF-8");//防止post乱码
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        User user=new User();
        user.setUsername(username);
        user.setPassword(password);
        final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
        final String DB_URL = "jdbc:mysql://localhost:3306/javam?serverTimezone=UTC&characterEncoding=utf-8";
        final String USER = "root";
        final String PASS = "12345678";

        Connection conn = null;
        Statement stmt = null;
        try{
            // 注册 JDBC 驱动
            Class.forName(JDBC_DRIVER);
            // 打开链接
            conn = DriverManager.getConnection(DB_URL,USER,PASS);
            // 执行查询
            stmt = conn.createStatement();
            String sql;
            sql = " SELECT * FROM user where username = '"+user.getUsername()+"' and password = '"+user.getPassword()+"'";
            ResultSet rs = stmt.executeQuery(sql);

            if(rs.next()){    //查不到数据 sql执行false
                String username2 = rs.getString("username");
                String password2  = rs.getString("password");
                System.out.println("查到了");
                if(user.getUsername().equals(username2) && user.getPassword().equals(password2)){
                    User usertemp = new User();
                    usertemp.setUsername(username2);
                    request.setAttribute("usertemp", usertemp);
                    request.getRequestDispatcher("/main.jsp").forward(request, response);
                }
            }
            else  {
                request.setAttribute("errInfo", "用户名或密码不正确！");
                System.out.println("没查到");
                request.getRequestDispatcher("/login.jsp").forward(request, response);
                //response.sendRedirect(request.getContextPath()+"/Login.jsp");
            }
            // 完成后关闭
            rs.close();
            stmt.close();
            conn.close();
        }catch(Exception se){
            // 处理 JDBC 错误
            se.printStackTrace();
        } finally {
            // 关闭资源
            try {
                if (stmt != null) stmt.close();
                if (conn != null) conn.close();
            }
            catch (Exception se) {
                se.printStackTrace();
            }
        }
    }

    protected void doGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {
        doPost(request,response);
    }
}
